<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.interfaces.cese.duss.ceseutil.WorkflowCreateHandler" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="rs1" class="weaver.conn.RecordSet"/>
<jsp:useBean id="rs2" class="weaver.conn.RecordSet"/>
<jsp:useBean id="bb" class="weaver.general.BaseBean"/>
<%
    bb.writeLog( "------------------------getSupplierBid_Ajax" );
    String id = Util.null2String( request.getParameter( "id" ) );//项目名称
    JSONObject jsonData = new JSONObject();
    String sql = "select id,zdzbrs,yqhzjzsj,lc,zbwj,tbjzrq,cglxing,fbzt,zbwjjxgfj1 from formtable_main_694 where id=?";
    rs.executeQuery( sql, id );
    bb.writeLog( sql );
    rs.next();
    boolean flag = true;
    String msg = "";
    String fbzt = Util.null2String( rs.getString( "fbzt" ) );//发标状态
    if ("0".equals( fbzt ) || "".equals( fbzt )) {
        String mainid = Util.null2String( rs.getString( "id" ) );
        int zdzbrs = Util.getIntValue( rs.getString( "zdzbrs" ) );//最低招标人数
        String yqhzjzsj = Util.null2String( rs.getString( "yqhzjzsj" ) );//邀请回执截止时间
        String lc = Util.null2String( rs.getString( "lc" ) );//轮次
        String zbwjjxgfj1 = Util.null2String( rs.getString( "zbwjjxgfj1" ) );//招标文件
        String tbjzrq = Util.null2String( rs.getString( "tbjzrq" ) );//投标截止日期
        String cglxing = Util.null2String( rs.getString( "cglxing" ) );//采购类型
        //获取采购需求ID
        if(cglxing.equals("0")){ //物料
            rs.executeQuery( "select wm_concat(cgxqid) cgxqid from formtable_main_694_dt1 where mainid=?", mainid );
        }else if(cglxing.equals("1")){ // 工程
            rs.executeQuery( "select wm_concat(cgxqid) cgxqid from formtable_main_694_dt2 where mainid=?", mainid );
        }else if(cglxing.equals("2")){ // 租赁
            rs.executeQuery( "select wm_concat(cgxqid) cgxqid from formtable_main_694_dt3 where mainid=?", mainid );
        }else if(cglxing.equals("3")){ //运输
            rs.executeQuery( "select wm_concat(cgxqid) cgxqid from formtable_main_694_dt7 where mainid=?", mainid );
        }else if(cglxing.equals("4")){ // 服务
            rs.executeQuery( "select wm_concat(cgxqid2) cgxqid from formtable_main_694_dt8 where mainid=?", mainid );
        }
        rs.next();
        String cgxqid = Util.null2String( rs.getString( 1 ) );
        bb.writeLog( "cgxqid=" + cgxqid );
        //判断标书评审是否归档
        String sql2 = "select b.currentnodetype from formtable_main_681 a \n" +
                "    inner join workflow_requestbase b \n" +
                "    on a.requestid = b.requestid  where nvl(a.requestid,0)<>0 and xmmc=?";
        rs.executeQuery( sql2, id );
        rs.next();

        String currentnodetype = Util.null2String( rs.getString( "currentnodetype" ) );//流程状态
        String currentDate = DateUtil.getCurrentDate();
        int res = yqhzjzsj.compareTo( currentDate );
        bb.writeLog( "res=" + res );

    /*if (res > 0) {
        jsonData.put( "flag", "邀请截止日期超过当前日期" );
    } else*/
        if (currentnodetype.equals( "3" )) { //流程状态为已归档的
            String creatorId = "";
            String workflowId = "34024";
            String requestName = "";
            String IsNextFlow = "0";
            Map<String, String> requestMainMap = new HashMap<>();
            WorkflowCreateHandler createHandler = new WorkflowCreateHandler();
            String sql3 = "select xmmc,xmbh,gysmc,gysmcrl from formtable_main_698  where xmmc = '" + id + "' and sfyb = '0' ";
            bb.writeLog( sql3 );
            rs.executeQuery( sql3 );
            int count = rs.getCounts();
            bb.writeLog( "count" + count );
            if (count >= zdzbrs) {
                while (rs.next()) {
                    String xmmc = Util.null2String( rs.getString( "xmmc" ) );//项目名称
                    String xmbh = Util.null2String( rs.getString( "xmbh" ) );//项目编号
                    String yqzz = Util.null2String( rs.getString( "gysmc" ) );//供应商名称
                    String lxr = Util.null2String( rs.getString( "gysmcrl" ) );//联系人员
                    creatorId = lxr;
                    requestName = "招采过程管理-供应商投标流程-" + lxr + "-" + DateUtil.getCurrentDate();
                    String rq = DateUtil.getCurrentDate();
                    requestMainMap.put( "rq", rq );//日期
                    requestMainMap.put( "xmmc", xmmc );//项目名称
                    requestMainMap.put( "xmbh", xmbh );//项目编号
                    requestMainMap.put( "bsfj", zbwjjxgfj1 );//招标文件
                    requestMainMap.put( "zbwj", zbwjjxgfj1 );//招标文件
                    requestMainMap.put( "tbjzrq", tbjzrq );//投标截止日期
                    requestMainMap.put( "cglx", cglxing ); //采购类型
                    requestMainMap.put( "lc", lc ); //轮次
                    requestMainMap.put( "gysmc", yqzz );//供应商名称
                    requestMainMap.put( "lxr", lxr );//联系人
                    String requestid = createHandler.WorkflowCreateByRequestMap( creatorId, workflowId, requestName, IsNextFlow, requestMainMap );
                    rs1.executeQuery( "select id,xmmc from formtable_main_674 where requestid=?", requestid );
                    rs1.next();
                    String mainid1 = Util.null2String( rs1.getString( "id" ) );
                    String dtlxmmc = Util.null2String( rs1.getString( "xmmc" ) );

                    rs1.executeQuery("select id,pjfs,zhpfswgz,swbl,jzj,jzjxs,zxplxs,fxplxs from uf_srm_pfgz where xm ='"+dtlxmmc+"'");
                    rs1.next();
                    String dtlid = Util.null2String( rs1.getString( "id" ) );//主表id
                    String pjfs = Util.null2String( rs1.getString( "pjfs" ) );//评价方式
                    String zhpfswgz = Util.null2String( rs1.getString( "zhpfswgz" ) );//综合评分商务规则
                    String swbl = Util.null2String( rs1.getString( "swbl" ) );//商务比例
                    String jzj = Util.null2String( rs1.getString( "jzj" ) );//	基准价
                    String jzjxs = Util.null2String( rs1.getString( "jzjxs" ) );//	基准价系数
                    String zxplxs = Util.null2String( rs1.getString( "zxplxs" ) );//	正向偏离系数
                    String fxplxs = Util.null2String( rs1.getString( "fxplxs" ) );//反向偏离系数

                    String sql7 = "update formtable_main_674 set pjfs =?,zhpfswgz=?,swbl=?,jzj=?,jzjxs=?,zxplxs=?,fxplxs=? where requestid =?";
                    rs1.executeUpdate(sql7,new Object[]{pjfs,zhpfswgz,swbl,jzj,jzjxs,zxplxs,fxplxs,requestid});

                    rs1.executeQuery("select fkfs,pfxs from  uf_srm_pfgz_dt1 where mainid ="+dtlid);
                    while (rs1.next()){
                        String fkfs = Util.null2String( rs1.getString( "fkfs" ) );
                        String pfxs = Util.null2String( rs1.getString( "pfxs" ) );

                        String sql8 = "insert into formtable_main_674_dt7 (mainid,fkfs,pfxs) values(?,?,?)";
                        rs1.executeUpdate(sql8,new Object[]{mainid1,fkfs,pfxs});
                    }

                    rs1.executeQuery("select fktj,pfxs from  uf_srm_pfgz_dt2 where mainid ="+dtlid);
                    while (rs1.next()){
                        String fktj = Util.null2String( rs1.getString( "fktj" ) );
                        String pfxs = Util.null2String( rs1.getString( "pfxs" ) );

                        String sql8 = "insert into formtable_main_674_dt8 (mainid,fktj,pfxs) values(?,?,?)";
                        rs1.executeUpdate(sql8,new Object[]{mainid1,fktj,pfxs});
                    }
                    String sql5 = "";
                    if(cglxing.equals("0")){ //物料
                        sql5 = "select t2.wlmc,t1.wlpl,t2.wlbh,t2.cms,t2.xlh,t2.xlbz,t2.dw,t2.sl from formtable_main_682 t1,formtable_main_682_Dt1 t2\n" +
                                " where t1.id = t2.mainid and t2.mainid in (" + cgxqid + ")";
                        bb.writeLog( sql5 );
                        rs2.executeQuery( sql5 );
                        while (rs2.next()) {
                            String wlmc = Util.null2String( rs2.getString( "wlmc" ) );//物料名称
                            String wlpl = Util.null2String( rs2.getString( "wlpl" ) );//物料品类
                            String wlbh = Util.null2String( rs2.getString( "wlbh" ) );//物料编号
                            String cms = Util.null2String( rs2.getString( "cms" ) );//长描述
                            String xlh = Util.null2String( rs2.getString( "xlh" ) );//序列号
                            String xlbz = Util.null2String( rs2.getString( "xlbz" ) );//序列备注
                            String dw = Util.null2String( rs2.getString( "dw" ) ); //单位
                            String sl = Util.null2String( rs2.getString( "sl" ) );//数量
                            String sql6 = "insert into formtable_main_674_dt1(mainid,wlpl,wlbh,wlmc,cms,xlh,xlbz,dw,sl) values(?,?,?,?,?,?,?,?,?)";
                            bb.writeLog( sql6 );
                            rs2.executeUpdate( sql6, new Object[]{mainid1, wlpl, wlbh, wlmc, cms, xlh, xlbz, dw, sl} );
                        }
                    }else if(cglxing.equals("1")){ //工程
                        sql5 = "select t2.wlbm,t2.wlmc,t2.cms,t2.xlh,t2.xlbz,t2.dw,t2.sl from formtable_main_685 t1,formtable_main_685_dt1 t2\n" +
                                " where t1.id = t2.mainid and t2.mainid in (" + cgxqid + ")";
                        bb.writeLog( sql5 );
                        rs2.executeQuery( sql5 );
                        while (rs2.next()) {
                            String wlbm = Util.null2String( rs2.getString( "wlbm" ) );//物料编码
                            String wlmc = Util.null2String( rs2.getString( "wlmc" ) );//物料名称
                            String cms = Util.null2String( rs2.getString( "cms" ) );//长描述
                            String xlh = Util.null2String( rs2.getString( "xlh" ) );//序列号
                            String xlbz = Util.null2String( rs2.getString( "xlbz" ) );//序列备注
                            String dw = Util.null2String( rs2.getString( "dw" ) ); //单位
                            String sl = Util.null2String( rs2.getString( "sl" ) );//数量
                            String sql6 = "insert into formtable_main_674_dt2(mainid,wlbm,wlmc,cms,xlh,xlbz,dw,sl) values(?,?,?,?,?,?,?,?)";
                            bb.writeLog( sql6 );
                            rs2.executeUpdate( sql6, new Object[]{mainid1, wlbm, wlmc, cms, xlh, xlbz, dw, sl} );
                        }
                    }else if(cglxing.equals("2")){ //租赁
                        sql5 = "select t2.mc,t2.xhgg,t2.dw,t2.sl,t2.dw1,t2.sj,t2.qssj,t2.jssj from formtable_main_687 t1,formtable_main_687_dt1 t2\n" +
                                " where t1.id = t2.mainid and t2.mainid in (" + cgxqid + ")";
                        bb.writeLog( sql5 );
                        rs2.executeQuery( sql5 );
                        while (rs2.next()) {
                            String mc = Util.null2String( rs2.getString( "mc" ) );//名称
                            String xhgg = Util.null2String( rs2.getString( "xhgg" ) );//型号规格
                            String dw = Util.null2String( rs2.getString( "dw" ) ); //单位
                            String sl = Util.null2String( rs2.getString( "sl" ) );//数量
                            String dw1 = Util.null2String( rs2.getString( "dw1" ) ); //时间单位
                            String sj = Util.null2String( rs2.getString( "sj" ) );//时间
                            String qssj = Util.null2String( rs2.getString( "qssj" ) );//起始时间
                            String jssj = Util.null2String( rs2.getString( "jssj" ) );//结束时间
                            String sql6 = "insert into formtable_main_674_dt4(mainid,mc,xhgg,sldw,sl,sjdw,sj,qssj,jssj) values(?,?,?,?,?,?,?,?,?)";
                            bb.writeLog( sql6 );
                            rs2.executeUpdate( sql6, new Object[]{mainid1, mc, xhgg, dw, sl, dw1, sj, qssj, jssj} );
                        }
                    }else if(cglxing.equals("3")){ //运输
                        sql5 = "select t2.hwmc,t2.ggxhckg,t2.cllf,t2.qydd,t2.dddd,t2.qysj,t2.dhrq,t2.ysfs,t2.sl from formtable_main_688 t1,formtable_main_688_dt1 t2\n" +
                                " where t1.id = t2.mainid and t2.mainid in (" + cgxqid + ")";
                        bb.writeLog( sql5 );
                        rs2.executeQuery( sql5 );
                        while (rs2.next()) {
                            String hwmc = Util.null2String( rs2.getString( "hwmc" ) );//货物名称
                            String ggxhckg = Util.null2String( rs2.getString( "ggxhckg" ) );//规格型号
                            String cllf = Util.null2String( rs2.getString( "cllf" ) );//重量、立方
                            String qydd = Util.null2String( rs2.getString( "qydd" ) );//启运地点
                            String dddd = Util.null2String( rs2.getString( "dddd" ) );//到达地点
                            String qysj = Util.null2String( rs2.getString( "qysj" ) );//启运时间
                            String dhrq = Util.null2String( rs2.getString( "dhrq" ) ); //到达时间
                            String ysfs = Util.null2String( rs2.getString( "ysfs" ) );//运输方式
                            String sl = Util.null2String( rs2.getString( "sl" ) );//数量
                            String sql6 = "insert into formtable_main_674_dt5(mainid,hwmc,ggxhckg,cllf,qydd,dddd,qysj,dhrq,ysfs,sl) values(?,?,?,?,?,?,?,?,?,?)";
                            bb.writeLog( sql6 );
                            rs2.executeUpdate( sql6, new Object[]{mainid1, hwmc, ggxhckg, cllf, qydd, dddd, qysj, dhrq, ysfs, sl} );
                        }
                    }else if(cglxing.equals("4")){ //服务
                        sql5 = "select t2.fwnr,t2.fwyq,t2.dw,t2.zlyq,t2.qsrq,t2.jsrq,t2.sl from formtable_main_690 t1,formtable_main_690_dt1 t2\n" +
                                " where t1.id = t2.mainid and t2.mainid in (" + cgxqid + ")";
                        bb.writeLog( sql5 );
                        rs2.executeQuery( sql5 );
                        while (rs2.next()) {
                            String fwnr = Util.null2String( rs2.getString( "fwnr" ) );//服务内容
                            String fwyq = Util.null2String( rs2.getString( "fwyq" ) );//服务要求
                            String dw = Util.null2String( rs2.getString( "dw" ) );//单位
                            String zlyq = Util.null2String( rs2.getString( "zlyq" ) );//资料要求
                            String qsrq = Util.null2String( rs2.getString( "qsrq" ) );//起始日期
                            String jsrq = Util.null2String( rs2.getString( "jsrq" ) );//结束日期
                            String sl = Util.null2String( rs2.getString( "sl" ) );//数量
                            String sql6 = "insert into formtable_main_674_dt6(mainid,fwnr,fwyq,dw,zlyq,qsrq,jsrq,sl) values(?,?,?,?,?,?,?,?)";
                            bb.writeLog( sql6 );
                            rs2.executeUpdate( sql6, new Object[]{mainid1, fwnr, fwyq, dw, zlyq, qsrq, jsrq, sl} );
                        }
                    }
                }
                flag = false;
                msg = "发标成功";
            } else {
                flag = false;
                msg = "投标人数少于最低投标人数";
            }
        } else {
            flag = false;
            msg = "标书评审暂未通过";
        }
    }else {
        flag = true;
        msg = "已发标，请勿重复发标";
    }
    jsonData.put( "flag", flag );
    jsonData.put( "msg", msg );
    out.print( jsonData.toJSONString() );
    bb.writeLog( "------------------jsonData=" + jsonData.toJSONString() );
%>
<script language="javascript" type="text/javascript">
    alert("<%=msg%>");
    window.close();
</script>

